package com.cruise.game.controller.order;

import com.cruise.game.common.api.ApiResult;
import com.cruise.game.common.constants.CommonConstants;
import com.cruise.game.controller.BaseController;
import com.cruise.game.model.param.order.AfterSalesParam;
import com.cruise.game.model.param.order.CreateOrderParam;
import com.cruise.game.model.param.order.RateOrderParam;
import com.cruise.game.model.vo.common.PageResult;
import com.cruise.game.model.vo.order.OrderDetailVO;
import com.cruise.game.model.vo.order.OrderListVO;
import com.cruise.game.service.order.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 订单控制器
 * 
 * @author cruise
 * @date 2025-02-28
 */
@Slf4j
@Api(tags = "订单相关接口")
@RestController
@RequestMapping("/api/orders")
public class OrderController extends BaseController {

    @Resource
    private OrderService orderService;

    /**
     * 创建订单
     */
    @ApiOperation("创建订单")
    @PostMapping
    public ApiResult<Map<String, Object>> createOrder(@Validated @RequestBody CreateOrderParam param) {
        OrderService.OrderCreationResult result = orderService.createOrder(param, getCurrentUserId());
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", result.getOrderId());
        map.put("orderNumber", result.getOrderNumber());
        map.put("price", result.getPrice());
        return ApiResult.success("下单成功", map);
    }

    /**
     * 获取订单列表
     */
    @ApiOperation("获取订单列表")
    @GetMapping
    public ApiResult<PageResult<OrderListVO>> getOrderList(
            @ApiParam(value = "订单状态", required = false, defaultValue = "all")
            @RequestParam(value = "status", required = false, defaultValue = "all") String status,
            
            @ApiParam(value = "页码", required = false, defaultValue = "1")
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            
            @ApiParam(value = "每页条数", required = false, defaultValue = "10")
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        
        // 限制最大页面大小
        if (pageSize > CommonConstants.MAX_PAGE_SIZE) {
            pageSize = CommonConstants.MAX_PAGE_SIZE;
        }
        
        return ApiResult.success(orderService.getUserOrderList(getCurrentUserId(), status, page, pageSize));
    }

    /**
     * 获取订单详情
     */
    @ApiOperation("获取订单详情")
    @GetMapping("/{orderId}")
    public ApiResult<OrderDetailVO> getOrderDetail(
            @ApiParam(value = "订单ID", required = true)
            @PathVariable("orderId") Long orderId) {
        return ApiResult.success(orderService.getUserOrderDetail(orderId, getCurrentUserId()));
    }

    /**
     * 取消订单
     */
    @ApiOperation("取消订单")
    @PostMapping("/{orderId}/cancel")
    public ApiResult<Boolean> cancelOrder(
            @ApiParam(value = "订单ID", required = true)
            @PathVariable("orderId") Long orderId) {
        return ApiResult.success("订单已取消", orderService.cancelOrder(orderId, getCurrentUserId()));
    }

    /**
     * 确认订单完成
     */
    @ApiOperation("确认订单完成")
    @PostMapping("/{orderId}/confirm")
    public ApiResult<Boolean> confirmOrder(
            @ApiParam(value = "订单ID", required = true)
            @PathVariable("orderId") Long orderId) {
        return ApiResult.success("订单已确认完成", orderService.confirmOrder(orderId, getCurrentUserId()));
    }

    /**
     * 申请售后
     */
    @ApiOperation("申请售后")
    @PostMapping("/{orderId}/after-sales")
    public ApiResult<Boolean> applyAfterSales(
            @ApiParam(value = "订单ID", required = true)
            @PathVariable("orderId") Long orderId,
            
            @Validated @RequestBody AfterSalesParam param) {
        return ApiResult.success("售后申请已提交", orderService.applyAfterSales(orderId, param, getCurrentUserId()));
    }

    /**
     * 评价订单
     */
    @ApiOperation("评价订单")
    @PostMapping("/{orderId}/rate")
    public ApiResult<Boolean> rateOrder(
            @ApiParam(value = "订单ID", required = true)
            @PathVariable("orderId") Long orderId,
            
            @Validated @RequestBody RateOrderParam param) {
        return ApiResult.success("评价成功", orderService.rateOrder(orderId, param, getCurrentUserId()));
    }
}
